package com.ybg.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class TextUtil {
	//文本替换函数
	public static String textReplace(String text,Map<String,Object> mapping){
		StringBuffer bf = new StringBuffer(text);
		Iterator<Entry<String,Object>> iter = mapping.entrySet().iterator();
		while(iter.hasNext()){
			Entry<String,Object> entry = iter.next();
			String key = entry.getKey();
			String value = String.valueOf(entry.getValue());
			int index = bf.indexOf(key);
			while(index!=-1){
				bf.replace(index,index+key.length(),value);
				index = bf.indexOf(key);
			}
		}
		return bf.toString();
	}
	//将list元素转换成一个sql的字段值范围()
	public static String convertListToRange(List<String> list){
		return convertListToRange(list, 0, list.size());
	}
	
	//将list元素转换成一个sql的字段值范围()
	public static String convertListToRange(List<String> list,int begin,int end){
		StringBuffer bf = new StringBuffer();
		if(list==null||list.size()==0){
			bf.append("('')");
		}else{
			bf.append("(");
			for(int i=begin;i<end;i++){
				bf.append("'"+list.get(i)+"',");
			}
			bf.replace(bf.lastIndexOf(","),bf.length(),")");
		}
		return bf.toString();
	}	
}
